package org.mch.func.m_px.yunsf;

import java.awt.event.MouseEvent;

import org.mch.biz.ui.BizEvent;
import org.mch.biz.ui.BizPanel;

public class AnWeiYiHuo_Event extends BizEvent {
	private static final long serialVersionUID = -589365474532268721L;
	private ToolBinaryFBY toolbinary = new ToolBinaryFBY();

	public AnWeiYiHuo_Event(BizPanel panel) {
		super(panel);
	}

	@Override
	protected void buttonClicked(MouseEvent e) throws Exception {
		if (e.getSource() == getPanel().getBtn_js_erjinzhi()) {
			onJsErJinZhi();
		} else if (e.getSource() == getPanel().getBtn_int_yihuo()) {
			onIntYiHuo();
		} else if (e.getSource() == getPanel().getBtn_clear()) {
			getPanel().getTextArea().setText("");
		}

		else if (e.getSource() == getPanel().getBtn_erjinzhi_yihuo()) {
			onErJinZhiYiHuo();
		}

		else if (e.getSource() == getPanel().getBtn_BumaToFanma()) {
			String alltext = getPanel().getTextArea().getText();
			String zhhang = alltext.substring(alltext.length() - 32, alltext.length());
			String s = toolbinary.byBuMaToFanMa(zhhang);
			getPanel().getTextArea().insert("\n\n" + s, alltext.length());
		} else if (e.getSource() == getPanel().getBtn_FanmaToBuma()) {
			String alltext = getPanel().getTextArea().getText();
			String zhhang = alltext.substring(alltext.length() - 32, alltext.length());
			String s = toolbinary.byFanMaToBuMa(zhhang);
			getPanel().getTextArea().insert("\n\n" + s, alltext.length());
		}

		else if (e.getSource() == getPanel().getBtn_FanmaToYuanma()) {
			String alltext = getPanel().getTextArea().getText();
			String zhhang = alltext.substring(alltext.length() - 32, alltext.length());
			String s = toolbinary.byFanMaToYuanMa(zhhang);
			getPanel().getTextArea().insert("\n\n" + s, alltext.length());
		} else if (e.getSource() == getPanel().getBtn_YuanmaToFanma()) {
			String alltext = getPanel().getTextArea().getText();
			String zhhang = alltext.substring(alltext.length() - 32, alltext.length());
			String s = toolbinary.byYuanMaToFanMa(zhhang);
			getPanel().getTextArea().insert("\n\n" + s, alltext.length());
		}
	}

	/**
	 * Java 取反运算符直接计算取反结果
	 */
	private void onIntYiHuo() {
		int ia = Integer.parseInt(getPanel().getText_int_a().getValue());
		int ib = Integer.parseInt(getPanel().getText_int_b().getValue());
		getPanel().getText_int_yihuo().setValue(ia ^ ib);
	}

	/**
	 * 计算二进制
	 */
	private void onJsErJinZhi() {
		int ia = Integer.parseInt(getPanel().getText_int_a().getValue());
		int ib = Integer.parseInt(getPanel().getText_int_b().getValue());

		String ia_ejz = Integer.toBinaryString(ia);
		ia_ejz = ia > 0 ? toolbinary.setMinssBinaryZero(ia_ejz) : ia_ejz;
		String ib_ejz = Integer.toBinaryString(ib);
		ib_ejz = ib > 0 ? toolbinary.setMinssBinaryZero(ib_ejz) : ib_ejz;

		getPanel().getText_ia_buma().setValue(ia_ejz);// 数 A 补码
		getPanel().getText_ib_buma().setValue(ib_ejz);// 数 B 补码

		String ia_fanma = toolbinary.byBuMaToFanMa(ia_ejz);
		String ib_fanma = toolbinary.byBuMaToFanMa(ib_ejz);
		getPanel().getText_ia_fanma().setValue(ia_fanma);// 数 A 反码
		getPanel().getText_ib_fanma().setValue(ib_fanma);// 数 B 反码

		getPanel().getText_ia_yuanma().setValue(toolbinary.byFanMaToYuanMa(ia_fanma));// 数A原码
		getPanel().getText_ib_yuanma().setValue(toolbinary.byFanMaToYuanMa(ib_fanma));// 数B原码

	}

	/**
	 * 二进制异或
	 */
	private void onErJinZhiYiHuo() {
		String ia_buma = getPanel().getText_ia_buma().getValue();
		String ib_buma = getPanel().getText_ib_buma().getValue();

		String sb_yh = toolbinary.tool_YiHuoIaAndIb(ia_buma, ib_buma);

		if (getPanel().getTextArea().getText().length() == 0) {
			getPanel().getTextArea().insert(sb_yh, getPanel().getTextArea().getText().length());
		} else {
			getPanel().getTextArea().insert("\n\n" + sb_yh, getPanel().getTextArea().getText().length());
		}
	}

	@Override
	public AnWeiYiHuo_Panel getPanel() {
		return (AnWeiYiHuo_Panel) super.getPanel();
	}
}
